//引入依赖文件
var express = require('express')
var app = express();
var db = require('./conn')
app.use((req, res, next) => {
    // 设置是否运行客户端设置 withCredentials
    // 即在不同域名下发出的请求也可以携带 cookie
    res.header("Access-Control-Allow-Credentials", true)
    // 第二个参数表示允许跨域的域名，* 代表所有域名  
    res.header('Access-Control-Allow-Origin', req.headers.origin)
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS') // 允许的 http 请求的方法
    // // 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
    // if (req.method == 'OPTIONS') {
    //     res.sendStatus(200)
    // } else {
    //     next()
    // }
    next()
})
//允许使用服务器端静态路径,app.use是调用中间件
// 解析 application/json
app.use(express.json())
// 解析 application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }));
app.use(express.static('./'))
//项目接口合集-------------------------------


//1.注册
app.post('/competition/user/reguser', (req, res) => {
    var addSql = 'select * from user where username=? '
    db.query(addSql, [req.body.username], (list, results, field) => {
        if (list) {
            /*判断数据库返回服务端的数据是否是空列表,
        如果是空列表 :则数据库没查到该用户名;否则就是查到了*/
            if (list.length != 0) {
                res.send({
                    success: false,
                    msg: '用户已存在，请重新注册！'
                })
            } else {
                console.log(list);
                //向数据库添加username和psw user_img
                var sql = 'insert into user(username,password) values(?,?)'
                db.query(sql, [req.body.username, req.body.password], (list, field) => {
                    if (list) {
                        res.send({ success: true, msg: '注册成功！' })
                    } else {
                        console.log('db' + list);
                        res.send({
                            success: true, msg: '注册失败,sql语句有误！'
                        })
                    }
                })
            }
        }

    })

})

//2.登录
app.post('/competition/user/login', (req, res) => {
    var sql = 'select * from user where username=? and password = ?'
    db.query(sql, [req.body.username, req.body.password], (list, field) => {
        if (list) {
            if (list.length != 0) {
                console.log(list.length);
                res.send({
                    success: true,
                    msg: '登录成功！'
                })
            } else {
                res.send({
                    success: false,
                    msg: '账号或密码错误,请重新输入'
                })
            }
        }
    })
})

//3.查询人员信息表staff
app.post('/competition/staff/list', (req, res) => {
    var sql = 'select * from staff'
    db.query(sql, [], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})

//4.查询团队信息表 team
app.post('/competition/team/list', (req, res) => {
    var sql = 'select * from team'
    db.query(sql, [], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})

//5.查询单练表project
app.post('/competition/project/list', (req, res) => {
    var sql = 'select * from team'
    db.query(sql, [], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})

//6.新增 人员：
app.post('/competition/staff/add', (req, res) => {
    var sql = 'insert into staff(statusname,job,idcard,username,sex,birthday,phone,email,vipdata,danxz,iddan,service,danbk,association,jiazhu,tour,souvenir,censusr,img) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);'
    db.query(sql, [req.body.statusname, req.body.job, req.body.idcard, req.body.username, req.body.sex, req.body.birthday, req.body.phone, req.body.email, req.body.vipdata, req.body.danxz, req.body.iddan, req.body.service, req.body.danbk, req.body.association, req.body.jiazhu, req.body.tour, req.body.souvenir, req.body.censusr, req.body.img], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '新增人员成功！'
            })
        } else {
            console.log('db' + list);
            res.send({
                success: false,
                msg: '新增人员失败！'
            })
        }
    })
})

//7.新增 团队信息：
app.post('/competition/team/add', (req, res) => {
    var sql = 'insert into team(teamname,teamabbreviation,englishname,teamaward,statelevel,nation,provinciallevel,citylevel,address,teamleader,phone,email,vxid,slogan,img) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);'
    db.query(sql, [req.body.teamname, req.body.teamabbreviation, req.body.englishname, req.body.teamaward, req.body.statelevel, req.body.nation, req.body.provinciallevel, req.body.citylevel, req.body.address, req.body.teamleader, req.body.phone, req.body.email, req.body.vxid, req.body.slogan, req.body.img], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '新增团队成功！'
            })
        } else {
            console.log('db' + list);
            res.send({
                success: false,
                msg: '新增团队失败！'
            })
        }
    })
})

//8.新增单练人员：
app.post('/competition/project/add', (req, res) => {
    var sql = 'insert into project(projectid,projectname,routinename,participant) values(?,?,?,?);'
    db.query(sql, [req.body.projectid, req.body.projectname, req.body.routinename, req.body.participant], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '新增单练人员成功！'
            })
        } else {
            console.log('db' + list);
            res.send({
                success: false,
                msg: '新增单练人员失败！'
            })
        }
    })
})

//9.查询首页表 home: res.data.list
app.post('/competition/home/list', (req, res) => {
    var sql = 'select * from home'
    db.query(sql, [], (list, field) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})



app.listen(3002, () => {
    // 3000是监听的端口号,JSP中经常使用8080端口，默认是80端口
    console.log('服务器启动！http://localhost:3002')
})